const { c0, $config, $fn } = getApp()
module.exports = Behavior({
    behaviors: ['wx://form-field-group'],
    properties: {
        data: null,
        label:null,
        name: String,
        placeholder: String,
        value: null,
        api: null,
        param: null,
        idStr: null,
        nameStr: null,
        mul: null
    },
    data: {
        c0,
        $config
    },
    lifetimes:{
        attached(){
            let { api } = this.data
            if(api){
                $http.pull(null, api, { param }).then(data=>{
                    this.setData({ data })
                })
            }
        }
    },
    methods: {
        change({ detail: { value } }){
            let { data, idStr, nameStr, mul } = this.data
            idStr = idStr ? idStr : $config.idStr
            nameStr = nameStr ? nameStr : $config.nameStr
            let rs
            let selectName
            let selectIndex
            if(mul){
                let stack = []
                rs = []
                for(let k in data){
                    stack.push( data[k][value[k]][nameStr] )
                    rs.push( data[k][value[k]][idStr] )
                }
                selectIndex = value
                selectName = stack.join('、')
            }else{
                selectIndex = +value
                selectName = data[selectIndex][nameStr]
                rs = data[selectIndex][idStr]
            }
            // 
            this.setData({ index:selectIndex, selectName })
            this.triggerEvent('change', rs)
        }
    },
    observers:{
        'value': function(value){
            if(value){
                setTimeout(()=>{
                    let { data, idStr, nameStr, mul } = this.data
                    idStr = idStr ? idStr : $config.idStr
                    nameStr = nameStr ? nameStr : $config.nameStr
                    let selectName
                    let selectIndex
                    if(mul){
                        if($fn.hasArray(value)){
                            selectIndex = []
                            let stack = []
                            for(let k in data){
                                let index = data[k].findIndex( v => {
                                    const bool = v[idStr] === value[k]
                                    if(bool){
                                        stack.push( v[nameStr] )
                                    }
                                    return bool
                                })
                                selectIndex.push(index)
                            }
                            selectName = stack.join('、')
                        }
                    }else{
                        selectIndex = data.findIndex( v => v[idStr] === value)
                        selectName = data[selectIndex][nameStr]
                    }
                    this.setData({ index:selectIndex, selectName })
                })
            }else{
                this.setData({ index:-1, selectName:'' })
            }
        }
    }
})